package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallPreferenceActivity;
import com.sc.nft.entity.dto.MallPreferenceActivityAdminDTO;
import com.sc.nft.entity.vo.MallPreferenceActivityAdminVO;
import com.sc.nft.enums.ProductTypeEnum;
import com.sc.nft.enums.task.ActivityTypeEnum;
import com.sc.nft.mapper.MallPreferenceActivityMapper;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 好物优选活动表(sc_mall_preference_activity)数据DAO
 *
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：MallPreferenceActivityDao
 * @Date：2024/3/11 14:33
 * @Filename：MallPreferenceActivityDao
 */
@Slf4j
@Repository
public class MallPreferenceActivityDao extends ServiceImpl<MallPreferenceActivityMapper, MallPreferenceActivity> {

    /**
     * 通过活动Id获取好物优选
     * @param activityId
     * @return
     */
    public MallPreferenceActivity getByActivityId(Long activityId) {
        LambdaQueryWrapper<MallPreferenceActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPreferenceActivity::getActivityId, activityId);
        wrapper.last("limit 1");
        List<MallPreferenceActivity> mallPreferenceActivityList = baseMapper.selectList(wrapper);
        return !CollectionUtils.isEmpty(mallPreferenceActivityList) ? mallPreferenceActivityList.get(0) : null;
    }

    /**
     * 获取已上架数量
     * @return
     */
    public Integer getTheNumberOfListings() {
        LambdaQueryWrapper<MallPreferenceActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPreferenceActivity::getStatus, Boolean.TRUE);
        wrapper.last("limit 1");
        List<MallPreferenceActivity> mallPreferenceActivities = baseMapper.selectList(wrapper);
        return !CollectionUtils.isEmpty(mallPreferenceActivities) ? mallPreferenceActivities.size() : 0;
    }

    public boolean updateStatus(Long id, Boolean status) {
        UpdateWrapper<MallPreferenceActivity> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        return update(new MallPreferenceActivity(), wrapper);
    }

    public boolean updateSort(Long id, Integer sort) {
        UpdateWrapper<MallPreferenceActivity> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("sort", sort);
        return update(new MallPreferenceActivity(), wrapper);
    }

    /**
     *
     * @param productName
     * @param daoId
     * @param productType
     * @param activityType
     * @param categoryId
     * @param status
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<MallPreferenceActivityAdminDTO> pageAdmin(String productName, Long daoId, ProductTypeEnum productType, ActivityTypeEnum activityType, Long categoryId, Boolean status, Integer pageNo, Integer pageSize) {
        return baseMapper.pageAdmin(new Page(pageNo, pageSize), productName, daoId, Objects.nonNull(productType) ? productType.getCode() : null, Objects.nonNull(activityType) ? activityType.getCode() : null, categoryId, status);
    }
}
